﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TextDiffAlgoCs
{
    public class CompareResult
    {
        private List<TextDiff> m_DiffList;

        public CompareResult()
        {
            m_DiffList = new List<TextDiff>();
        }

        public List<TextDiff> getDiffList
        {
            get { return m_DiffList; }
        }

        public double GetCompareRes(string before, string after)
        {
            List<TextDiff> diffs = TextComparer.Compare(before, after);
            m_DiffList = new List<TextDiff>(diffs);
            TextDiff[] diffArrays = new TextDiff[diffs.Count];
            diffArrays = diffs.ToArray();
            Array.Sort(diffArrays);

            int deleteIndex = 0, insertIndex = 0;
            int same = 0;
            string content, keeped;

            foreach (TextDiff diff in diffArrays)
            {
                if (diff.getDiffType == TextDiff.TYPE_DELETE)
                {
                    if (deleteIndex < diff.getDiffStartIndex && diff.getDiffStartIndex < before.Length)
                    {
                        keeped = before.Substring(deleteIndex, diff.getDiffStartIndex - deleteIndex);
                        Console.WriteLine("RemainA:" + keeped);
                        m_DiffList.Add(new TextDiff(TextDiff.TYPE_SAME_A, deleteIndex, diff.getDiffStartIndex - deleteIndex));
                        same += diff.getDiffStartIndex - deleteIndex;
                        insertIndex += diff.getDiffStartIndex - deleteIndex;
                        deleteIndex = diff.getDiffStartIndex;
                    }
                    content = before.Substring(diff.getDiffStartIndex, diff.getDiffLength);
                    Console.WriteLine("Delete: " + content + "\t" + diff.ToString());
                    deleteIndex += diff.getDiffLength;
                }
                else if (diff.getDiffType == TextDiff.TYPE_INSERT)
                {
                    if (insertIndex < diff.getDiffStartIndex && diff.getDiffStartIndex < after.Length)
                    {
                        keeped = after.Substring(insertIndex, diff.getDiffStartIndex - insertIndex);
                        Console.WriteLine("RemainB:" + keeped);
                        m_DiffList.Add(new TextDiff(TextDiff.TYPE_SAME_B, insertIndex, diff.getDiffStartIndex - insertIndex));
                        same += diff.getDiffStartIndex - insertIndex;
                        deleteIndex += diff.getDiffStartIndex - insertIndex;
                        insertIndex = diff.getDiffStartIndex;
                    }
                    content = after.Substring(diff.getDiffStartIndex, diff.getDiffLength);
                    Console.WriteLine("Insert: " + content + "\t" + diff);
                    insertIndex += diff.getDiffLength;
                }
            }
            m_DiffList.Sort();

            Console.WriteLine("*****TEST*****");
            foreach (TextDiff td in m_DiffList)
            {
                Console.WriteLine(td.ToString());
            }
            Console.WriteLine(same);
            Console.WriteLine(before.Length);
            Console.WriteLine(after.Length);
            Console.WriteLine("*****OVER*****");

            if (diffArrays.Length == 0)
            {
                return 1.0;
            }

            // return strange value
            return (double) same * 2.0 / (double) (before.Length + after.Length);
        }
    }
}
